var main = {
    txt: $('.mainInput'),
    //初始化
    init: function(){
        this.txt.focus();
        this.txt.on('keyup', function(event){
            var e = event || window.event || arguments.callee.caller.arguments[0];
            main.keyUp(e);
        })
    },
    //键盘事件
    keyUp: function(e){
        if(e && e.keyCode === 13){
            main.getVal();
        }else {
            clearTimeout(this.tid);
            this.tid = setTimeout(function() {
                main.getVal();
            }, 4500);
        }
    },
    //获取输入内容
    getVal: function(){
        var val = this.txt.val();
        if (val.length == 0 || (/^\s+$/g).test(val)){
            //输入为空 or 输入全是空格
        }else{
            main.getRes(val);
        }
    },
    //获取数据
    getRes: function(val){
        $.ajax({
            type: "get",
            'url': 'http://a.modian-tech.com/search?keywords=' + val,
            success: function (data){
                var res = data.langs , $domArr=[];
                if(res.length >= 1){
                    for (var i = 0; i < res.length; i++ ) {
                        $domArr.push($('#'+res[i]));
                    }
                    main.showIcon($domArr);
                }
            },
            'error':function () {
                alert("请求失败！");
            }
        });
    },
    //显示图标
    showIcon: function($domArr){
        $('.logo').animate({ textAlign: 'left', 'top': 30, 'left': 0 }, 1000);
        $('.logo h1').animate({ fontSize: '100px' }, 1000);
        $('.mainInput').animate({ margin: '-320px 0 0 -50px', 'left':460 }, 1000);
        for (var i = 0; i < $domArr.length; i++ ) {
            $domArr[i].fadeIn(200).animate({ top: '350px', opacity: '1', left: i*320+'px' }, 1000);
            main.showArticle($domArr[i]);
        }
    },
    //显示文章
    showArticle: function(dom){
        dom.click(function(){
            $('.icons>div').fadeOut(200).animate({ opacity: '0', zIndex: 1 }, 1000);
            $('.main').fadeIn(200).animate({ opacity: '1', zIndex: 88 }, 1000);
        })
    }
};

window.onload = function(){
    main.init();
};